package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.storage.domain.order.domain.ConverterManagement;
import com.pureut.storage.dto.ConverterMqDto;
import com.pureut.storage.mapper.ConverterManagementMapper;
import com.pureut.storage.service.ConverterManagementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/***
 *
 * Author:C
 * Date:2023/01/19 17:15
 * @DESC
 */
@Service
public class ConverterManagementServiceImpl extends ServiceImpl<ConverterManagementMapper, ConverterManagement> implements ConverterManagementService {

    @Resource
    ConverterManagementMapper converterManagementMapper;

    /**
     * 根据mq新增
     *
     * @param converterMqDto
     * @return
     */
    @Override
    public boolean insertConverterManagement(ConverterMqDto converterMqDto) {
        if (converterMqDto.getConverterNum() != null) {
            //判断该编码在表中是否存在
            ConverterManagement converterManagement1 = converterManagementMapper.selectOne(new QueryWrapper<ConverterManagement>().lambda().eq(ConverterManagement::getConverterNum, converterMqDto.getConverterNum()));
            if (converterManagement1 == null) {
                ConverterManagement converterManagement = new ConverterManagement();
                BeanUtils.copyProperties(converterMqDto, converterManagement);
                return save(converterManagement);
            }
        }
        return false;
    }

    /**
     * 根据ma修改
     *
     * @param converterMqDto
     * @return
     */
    @Override
    public boolean updateConverterManagement(ConverterMqDto converterMqDto) {

        ConverterManagement converterManagement = converterManagementMapper.selectOne(new QueryWrapper<ConverterManagement>().lambda().eq(ConverterManagement::getConverterNum, converterMqDto.getConverterNum()));

        BeanUtils.copyProperties(converterMqDto, converterManagement);


        return updateById(converterManagement);
    }

    @Override
    public void deleteConverterManagement(ConverterMqDto mq) {
        if (mq.getConverterNum() != null) {
            List<String> codes = Arrays.asList(mq.getConverterNum().split(","));
            List<ConverterManagement> converterManagements = converterManagementMapper.selectList(new QueryWrapper<ConverterManagement>().lambda().in(ConverterManagement::getConverterNum, codes));
            if (converterManagements.size() > 0) {
                removeByIds(converterManagements);
            }
        }
    }
}
